package com.vcredit.creditcat.web.api;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.vcredit.creditcat.redis.RedisServiceExt;
import com.vcredit.framework.util.VerifyCodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.session.Session;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.vcredit.creditcat.model.Cust;
import com.vcredit.framework.controller.BaseController;

import static com.vcredit.creditcat.util.Constants.SMS_EXPIRE_TIME;

public class BaseCreditCatController extends BaseController {
	@Value("${project.env}")
	private String env;
	@Autowired
	private RedisServiceExt redisService;


	protected Session getSession(HttpServletRequest request) {
		return (Session) request.getSession().getAttribute("session");
	}

	protected Cust getCust(HttpServletRequest request) {
		Cust cust = new Cust(3L, 18L);
		if (StringUtils.isEmpty(cust.getCustIdno())) {
			cust.setCustIdno("payDay");
			cust.setCustName("payDay");
		}
		return cust;
	}

	/**
	 * 获取Cust
	 * 
	 * @author lixiaoqiang
	 */
	protected Cust getCust() {
		return getCust(getRequest());
	}

	/**
	 * 获取request
	 * 
	 * @author lixiaoqiang
	 */
	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 获取session
	 * 
	 * @author lixiaoqiang
	 */
	protected HttpSession getSessions() {
		return getRequest().getSession();
	}

	/**
	 * 获取request参数 封装Map
	 * 
	 * @author song.lixiaoqiang
	 */
	public static Map<String, String> getAllRequestParam(HttpServletRequest request) {
		Map<String, String> res = new HashMap<String, String>();
		Enumeration<?> temp = request.getParameterNames();
		if (null != temp) {
			while (temp.hasMoreElements()) {
				String en = (String) temp.nextElement();
				String value = request.getParameter(en);
				res.put(en, value);
				// 在报文上送时，如果字段的值为空，则不上送<下面的处理为在获取所有参数数据时，判断若值为空，则删除这个字段>
				if (res.get(en) == null || "".equals(res.get(en))) {
					res.remove(en);
				}
			}
		}
		return res;
	}
	/**
	 * 创建验证码
	 *
	 * @param request
	 * @param mobile
	 *            手机
	 * @param businessType
	 *            验证类型 1:登陆 2:绑定手机 3:绑定银行卡 4:渠道注册页面
	 * @return
	 */
	protected String createVerifyCode(HttpServletRequest request, String mobile, Integer businessType) {
		String code = VerifyCodeUtil.getVerifyCode(true).getCode();
		String key = "vercode:"+request.getRequestedSessionId() + "_" + mobile + "_" + businessType;
		if (redisService.get(key) != null) {
			redisService.del(key);
		}
		redisService.put(key, code, SMS_EXPIRE_TIME);
		return code;
	}
	/**
	 * 校验验证码
	 * @param request
	 * @param mobile
	 * @param code
	 * @param businessType
	 * @return
	 */
	protected Boolean checkVerifyCode(HttpServletRequest request, String mobile, String code, Integer businessType) {
		if (!StringUtils.equalsIgnoreCase("prd", env)) {
			return true;
		} else {
			String key = "vercode:"+request.getRequestedSessionId() + "_" + mobile + "_" + businessType;
			Object object = redisService.get(key);
			if (object == null) {
				return false;
			} else {
				return object.equals(code);
			}
		}
	}
	protected void clearSession(HttpServletRequest request) {
		request.getSession().removeAttribute("session");
	}
}
